package cn.tedu.csmallproducts.controller;


import cn.tedu.csmallproducts.pojo.dto.CategoryAddNewDTO;

import cn.tedu.csmallproducts.pojo.dto.CategoryUpdateDTO;
import cn.tedu.csmallproducts.pojo.vo.AlbumStandardVO;
import cn.tedu.csmallproducts.pojo.vo.CategoryListItemVO;
import cn.tedu.csmallproducts.pojo.vo.CategoryStandardVO;
import cn.tedu.csmallproducts.service.ICategoryService;
import cn.tedu.csmallproducts.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/categories")
@Api(tags = "03.类别管理模块")
@Validated
public class CategoryController {
    @Autowired
    private ICategoryService categoryService;

    @PostMapping("/add-new")
    @ApiOperation("添加类别")
    @ApiOperationSupport(order = 100)
    public JsonResult<Void> addNew(@Valid CategoryAddNewDTO categoryAddNewDTO){
        log.debug("开始处理【添加类别】的请求，参数：{}", categoryAddNewDTO);
            categoryService.addNew(categoryAddNewDTO);
            return JsonResult.ok();

    }

    @GetMapping("/list-by-parent")
    @ApiOperation("根据父级查询子级列表")
    @ApiOperationSupport(order = 420)
    public JsonResult<List<CategoryListItemVO>> list(Long parentId) {
        log.debug("开始处理【根据父级类别查询子级类别列表】的业务，参数：{}", parentId);
        List<CategoryListItemVO> list = categoryService.listByParentId(parentId);
        return JsonResult.ok(list);
    }

    @PostMapping("/{id:[0-9]+}/delete")
    @ApiOperation("根据ID删除类别")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParam(name = "id",value = "类别ID",required = true,dataType = "long")
    public JsonResult<Void> delete(@PathVariable @Range(min = 1,message = "请提交有效的ID值！") Long id){
        categoryService.delete(id);
        return JsonResult.ok();

    }

    @PostMapping("/{id:[0-9]+}/disable")
    @ApiOperation("禁用类别")
    @ApiOperationSupport(order = 311)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "类别ID", required = true, dataType = "long")
    })
    public JsonResult<Void> setDisable(@PathVariable @Range(min = 1, message = "请提交有效的ID值！") Long id) {
        log.debug("开始处理【禁用类别】的请求，参数：{}", id);
        categoryService.setDisable(id);
        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/enable")
    @ApiOperation("启用类别")
    @ApiOperationSupport(order = 310)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "类别ID", required = true, dataType = "long")
    })
    public JsonResult<Void> setEnable(@PathVariable @Range(min = 1, message = "请提交有效的ID值！") Long id) {
        log.debug("开始处理【启用类别】的请求，参数：{}", id);
        categoryService.setEnable(id);
        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/display")
    @ApiOperation("显示在导航栏")
    @ApiOperationSupport(order = 313)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "类别ID", required = true, dataType = "long")
    })
    public JsonResult<Void> setDisplay(@PathVariable @Range(min = 1, message = "请提交有效的ID值！") Long id) {
        log.debug("开始处理【显示在导航栏】的请求，参数：{}", id);
        categoryService.setDisplay(id);
        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/blank")
    @ApiOperation("不显示在导航栏")
    @ApiOperationSupport(order = 314)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "类别ID", required = true, dataType = "long")
    })
    public JsonResult<Void> setBLANK(@PathVariable @Range(min = 1, message = "请提交有效的ID值！") Long id) {
        log.debug("开始处理【不显示在导航栏】的请求，参数：{}", id);
        categoryService.setBLANK(id);
        return JsonResult.ok();
    }

    @GetMapping("/{id:[0-9]+}")
    @ApiOperation("根据ID查询类别详情")
    @ApiOperationSupport(order = 410)
    public JsonResult<CategoryStandardVO> getStandardById(
            @PathVariable @Range(min = 1, message = "请提交有效的ID值！") Long id) {
        log.debug("开始处理【根据ID查询类别详情】的请求，参数：{}", id);
        CategoryStandardVO queryResult = categoryService.getStandardById(id);
        return JsonResult.ok(queryResult);
    }

    @PostMapping("/{id:[0-9]+}/update")
    @ApiOperation("修改类别详情")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "类别ID", required = true, dataType = "long")
    })
    public JsonResult<Void> updateInfoById(@PathVariable @Range(min = 1, message = "请提交有效的ID值！") Long id,
                                           @Valid CategoryUpdateDTO categoryUpdateDTO) {
        log.debug("开始处理【修改类别详情】的请求，ID：{}，新数据：{}", id, categoryUpdateDTO);
        categoryService.updateInfoById(id, categoryUpdateDTO);
        return JsonResult.ok();
    }



}
